#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1e5 + 10, M = 5e5 + 10, INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
#define x first
#define y second
int h[N], e[M], ne[M], w[M], idx;
bool st[N];
int dis[N];
int n, m, s, t;
void add(int a, int b, int c)
{
	e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
void dij()
{
	memset(dis, 0x3f, sizeof(dis));
	dis[s] = 0;
	priority_queue<PII, vector<PII>, greater<PII>>heap;
	heap.push({ 0,s });
	while (heap.size()) {
		PII t = heap.top();
		heap.pop();
		int distance = t.x;
		int ver = t.y;
		if (st[ver])continue;
		st[ver] = 1;
		for (int i = h[ver]; ~i; i = ne[i]) {
			int j = e[i];
			if (dis[j] > distance + w[i]) {
				dis[j] = distance + w[i];
				heap.push({ dis[j],j });
			}
		}
	}
}
int main()
{
	cin >> n >> m >> s >> t;
	memset(h, -1, sizeof(h));
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
		add(b, a, c);
	}
	dij();
	if (dis[t] == INF)dis[t] = -1;
	cout << dis[t] << endl;
	return 0;
}